# -*- coding: utf-8 -*-
import requests
import json

from flask import Blueprint
from flask import request
from flask import Response

baiduservice = Blueprint('baidu', __name__, )

baidukey="24aace7725367ec6355a7b894a2e75b7"

def baidugeocode(address):
   
    s = requests.session() 
    url= 'http://api.map.baidu.com/geocoder/v2/?address='+address+'&output=json&ak='+baidukey

    header = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'}
    response = s.get(url, headers = header, timeout = 20)
    print(response.text) 
    geodata = json.loads(response.text)
    return json.dumps(geodata)
 

@baiduservice.route("/rest/baidu/geocoder", methods=['POST', 'GET'])
def geocoder():
    goedata=baidugeocode(request.args.get('address'))
    return goedata

@baiduservice.route("/rest/baidu/rout", methods=['POST', 'GET'])
def georout():
    location=baidugeocode(request.args.get('location')) #35.658651,139.745415
   
    s = requests.session()
     
    url= 'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location='+location+'&output=json&pois=1&ak='+baidukey
    header = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'}

    response = s.get(url, headers = header, timeout = 20)
    print(response.text) 
    geodata = json.loads(response.text)
    return json.dumps(geodata)


@baiduservice.route("/rest/baidu/rout1", methods=['POST', 'GET'])
def georout1():
    origin=baidugeocode(request.args.get('origin'))
    destination=baidugeocode(request.args.get('destination'))
 
    s = requests.session()
    #url = 'http://api.map.baidu.com/highacciploc/v1?qcip=139.214.254.47&qterm=pc&ak='+baidukey+'&coord=bd09ll&extensions=3'
    url= 'http://api.map.baidu.com/direction/v2/transit?origin='+origin+'&destination='+destination+'&ak='+baidukey
    header = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'}

    response = s.get(url, headers = header, timeout = 20)
    print(response.text) 
    geodata = json.loads(response.text)
    return json.dumps(geodata)


